package com.fh.modules.dbms.service.impl;

import com.fh.db.DbSource;
import com.fh.modules.dbms.entity.DbmsDbsource;
import com.fh.modules.dbms.entity.DbmsDbtype;
import com.fh.modules.dbms.mapper.DbmsDbsourceMapper;
import com.fh.modules.dbms.service.IDbmsDbsourceService;
import com.fh.modules.dbms.service.IDbmsDbtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fh.common.web.impl.AbstractServiceImpl;

/**
 * @Description: 数据源管理
 * @author： code generator
 * @date：   2020-05-15
 * @version： V1.0
 */
@Service
public class DbmsDbsourceServiceImpl extends AbstractServiceImpl<DbmsDbsourceMapper, DbmsDbsource> implements IDbmsDbsourceService {

    @Autowired
    private IDbmsDbtypeService dbmsDbtypeService;

    @Override
    public DbSource getDbSource(Integer dbmsSourceId) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        DbmsDbsource dbmsDbsource=this.getById(dbmsSourceId);
        DbmsDbtype dbmsDbtype=dbmsDbtypeService.getById(dbmsDbsource.getTypeId());
        DbSource dbSource= (DbSource) Class.forName(dbmsDbtype.getSourceClass()).newInstance();
        dbSource.initSource(dbmsDbtype.getTypeCode(),dbmsDbsource.getUrl(),dbmsDbsource.getUserName(),dbmsDbsource.getPassword(),dbmsDbtype.getJdbcDriver());
        return dbSource;
    }
}
